package Controllers

import (
	"fmt"
	"os"
	"strconv"
	"sync"

	"github.com/gin-gonic/gin"
)

type TestController struct {
	commonController
}

type players struct {
	Number int
	UserId int
	Lock   sync.RWMutex
}

var a players
var b sync.WaitGroup

func (that *TestController) Index(r *gin.Context) {
	// var a1 []interface{}
	// a2 := []int{1, 2, 3}
	// a3 := append(a1, a2)
	// fmt.Println(len(a3))
	// a.Number = 5
	// bingfaliang := 100
	// b.Add(bingfaliang)

	// a.UserId = 0

	// for ii := 0; ii < bingfaliang; ii++ {
	// 	go func(ii int) {
	// 		wefValue()
	// 	}(ii)
	// }

	// //模拟并发
	// for i := 0; i < bingfaliang; i++ {
	// 	go func() {
	// 		refValue()
	// 		defer func() {
	// 			b.Done()
	// 		}()
	// 	}()
	// }

	// b.Wait()
	// a.Lock.RLock() //安全读
	// // User_id, _ := json.Marshal(a.UserId)
	// data := map[string]any{}
	// for key, value := range a.UserId {
	// 	data[key] = value
	// }
	// // json.Unmarshal(User_id, &data)
	// a.Lock.RUnlock()

	that.SendSuccessResponse(r, os.Getpid(), "")
	// bb := []int{1, 2, 3, 4, 5}
	// aa := []int{}
	// copy(bb, aa)
	// fmt.Printf("%p", bb)
	// wefValue(aa)
	// fmt.Println(bb)
}

func wefValue() {
	a.Lock.Lock()
	data := a.UserId
	fmt.Println("lock2 " + strconv.Itoa(data))
	a.UserId = data + 1
	defer a.Lock.Unlock()
}

func refValue() {
	// a.Lock.RLock()

	// a.Lock.RUnlock()
	a.Lock.Lock()
	data := a.UserId
	fmt.Println("lock  " + strconv.Itoa(data))
	a.UserId = data + 1
	defer a.Lock.Unlock()
}
